import $axios from '@/plugins/axios'

export const state = () => ({
  id: '',
  name: '',
  bio: '',
  image: ''
})
export const mutations = {
  get_user(state, res) {
    state.id = res.data[0].id
    state.name = res.data[0].name
    state.bio = res.data[0].bio
    state.image = res.data[0].image
  },
  resetUser(state) {
    state.id = ''
    state.name = ''
    state.email = ''
    state.bio = ''
    state.image = ''
  },
  likePost(state, {post, isPostPage, res}) {
    let singlePost = null
    if (isPostPage) {
      singlePost = post
    } else {
      singlePost = this.state.general.posts.find(p => p.id === post.id)
    }
    singlePost.likes.push(res.data.like)
  },
  unlikePost(state, {post, isPostPage, id}) {
    let deleteLike = null
    let singlePost = null
    if (isPostPage) {
      singlePost = post
    } else {
      singlePost = this.state.general.posts.find(p => p.id === post.id)
    }
    singlePost.likes.forEach(like => {
      if (like.user_id === state.id) {
        deleteLike = like
      }
    });
    for (let i = 0; i < singlePost.likes.length; i++) {
      const like = singlePost.likes[i];
      if (like.id === id) {
        singlePost.likes.splice(i, 1);
      }
    }
  },
  updateComments(state, updatePost) {
    this.state.general.selectedPost.comments = updatePost.comments

  }
}
export const actions = {
  async getTokens() {
    await this.$axios.get('/sanctum/csrf-cookie')
  },

  async login({commit}, {email, password}) {
    await this.$axios.post('/login', {
      email: email,
      password: password
    })
  },

  async register({commit}, {name, email, password, confirmPassword}) {
    await this.$axios.post('/register', {
      name: name,
      email: email,
      password: password,
      password_confirmation: confirmPassword
    })
  },
  async getUser({commit}) {
    let res = await this.$axios({
      method:'get',
      url: '/api/logged-in-user'
    })
    commit('get_user', res)
  },

  async updateUserImage({}, data) {
    return await this.$axios.post('/api/update-user-image', data)
  },

  async updateUser({}, {name, bio}) {
    return await this.$axios.patch('/api/update-user', {
      name: name,
      bio: bio
    })
  },

  async createPost({}, data) {
    return await this.$axios.post('/api/posts', data)
  },

  async deletePost({}, post) {
    return await this.$axios.delete(`/api/posts/${post.id}`)
  },

  async addComment({dispatch}, {post, comment}) {
    let res = await this.$axios.post('/api/comments', {
      post_id: post.id,
      comment: comment
    })
    if (res.status === 200) {
      await dispatch('updateComments', post)
    }
  },

  async deleteComment({dispatch}, {post, commentId}) {
    let res = await this.$axios.delete(`/api/comments/${commentId}`, {
      post_id: post.id
    })
    if (res.status === 200) {
      await dispatch('updateComments', post)
    }
  },

  async updateComments({commit}, post) {
    let res = await this.$axios.get(`/api/profiles/${post.user.id}`)

    for (let i = 0; i < res.data.posts.length; i++) {
      const updatePost = res.data.posts[i];
      if (post.id == updatePost.id) {
        commit('updateComments', updatePost)
      }
    }
  },

  async likePost({commit}, {post, isPostPage}) {
    let res = await this.$axios.post('/api/likes', {
      post_id: post.id,
    })
    commit('likePost', {post, isPostPage, res})
  },

  async unlikePost({commit, rootState, state}, {post, isPostPage}) {
    let deleteLike = null
    let singlePost = null
    if (isPostPage) {
      singlePost = post
    } else {
      singlePost = rootState.general.posts.find(p => p.id === post.id)
    }
    singlePost.likes.forEach(like => {
      if (like.user_id === state.id) {
        deleteLike = like
      }
    });
    let res = await this.$axios.delete('/api/likes/' + deleteLike.id)
    commit('unlikePost', {post, isPostPage, id: res.data.like.id})
  },

  async logout({commit}) {
    await this.$axios.post('/logout')
    commit('resetUser')
  },
}
